package com.watson.thread.concurrent;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @program: mytest
 * @description: lock test
 * @author: zhangpeng348@jd.com
 * @date: 2018-12-18 20:56
 **/
@Component
//@Scope("prototype")
public class LockTest {

    public static void main(String[] args) {

        ThreadLocalRandom current = ThreadLocalRandom.current();
        ThreadLocal<String> localStr = new ThreadLocal<>();
        for (int i = 0; i < 5; i++) {
            new Thread() {
                @Override
                public void run() {
                    System.out.println("thread id: " + Thread.currentThread().getId() + "current id: " + ThreadId.get());
                }
            }.start();
        }
    }

    static class ThreadId {
        private static int a = 0;
        private static final AtomicInteger NEXT_ID;
        private static final ThreadLocal<Integer> THREA_ID;
        static {
            NEXT_ID = new AtomicInteger(0);

            THREA_ID = new ThreadLocal<Integer>() {
//                @Override
//                protected Integer initialValue() {
//                    return NEXT_ID.incrementAndGet();
//                }
            };
        }

        public static int get() {
//            THREA_ID.set(2);
            return THREA_ID.get();

        }
    }

    public void test() {
        System.out.println("abc");

    }
}
